<!DOCTYPE html>
  <html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    /**
     * 实现 流程控制 
      LazyMan(“Hank”).eat(“dinner”).eat(“supper”)输出
      Hi This is Hank!
      Eat dinner~
      Eat supper~
    */
   
    class _LazyMan{
      constructor(name){
        // 任务队列
        this.tasks = []
        // 任务1
        let task = ()=>{
          console.log("Hi,This is "+name)
          this.next()
        }
        this.tasks.push(task)
        setTimeout(() => {
          this.next()
        }, 0);
      }
      next(){
        let task = this.tasks.shift()
        task && task()
      }
      sleep(time){
        let task = ()=>{
          setTimeout(() => {
            console.log("sleep")
            this.next()
          }, time*1000);
        }
        this.tasks.push(task)
        return this
      }
      eat(str){
        let task = ()=>{
          console.log("eat，"+ str)
          this.next()
        }
        this.tasks.push(task)
        return this
      }
    }
    function LazyMan(name){
      return new _LazyMan(name)
    }
    LazyMan('Hank').sleep(2).eat('dinner')
  </script>
</body>
</html>